﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Pomelo.EntityFrameworkCore.MySql;

namespace MultiTenantDemo.Context
{
    public class TestDbContext : DbContext
    {
        private readonly IConnectionResolve _connectionResolve;

        public TestDbContext(IConnectionResolve connectionResolve, DbContextOptions dbContextOptions) : base(dbContextOptions)
        {
            _connectionResolve = connectionResolve;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var entityTypeList = typeof(Program).Assembly.GetExportedTypes().Where(m => m.IsClass && !m.IsAbstract && typeof(Entity.EntityBase).IsAssignableFrom(m)).ToList();
            foreach (var item in entityTypeList)
            {
                modelBuilder.Model.AddEntityType(item);
            }
            string tt = modelBuilder.Model.GetEntityTypes().ToList()[0].ShortName();

            base.OnModelCreating(modelBuilder);
        }

        //public async override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        //{
        //    //ChangeTracker.DetectChanges();
        //    //ChangeTracker.

        //    return await base.SaveChangesAsync(default);
        //}
    }
}
